package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.CommonDialog;
import com.fgs.common.interf.ISelectItemValue;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.interf.ISelectTimeData;
import com.tianhui.consignortwofei.mvp.ui.adapter.SelectLeftTimeAdapter;
import com.tianhui.consignortwofei.mvp.ui.adapter.SelectRightTimeAdapter;

import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

/**
 * @author fgs
 * @date 2021/4/22
 * @description
 **/
public class SelectTimeDialog extends CommonDialog {

    private TextView cancelTextView;
    private TextView titleTextView;
    private TextView confirmTextView;
    private TextView tipTextView;
    private RecyclerView leftRecyclerView;
    private RecyclerView rightRecyclerView;
    private SelectLeftTimeAdapter leftTimeAdapter;
    private SelectRightTimeAdapter rightTimeAdapter;
    private ISelectTimeData iSelectTimeData;
    private OnSelectTimeConfirmClickListener onSelectTimeConfirmClickListener;

    public SelectTimeDialog(@NonNull Context context, ISelectTimeData iSelectTimeData) {
        super(context, R.style.style_select_dialog);
        this.iSelectTimeData = iSelectTimeData;
    }

    public void setISelectTimeData(ISelectTimeData iSelectTimeData) {
        this.iSelectTimeData = iSelectTimeData;
    }

    public void setOnSelectTimeConfirmClickListener(OnSelectTimeConfirmClickListener onSelectTimeConfirmClickListener) {
        this.onSelectTimeConfirmClickListener = onSelectTimeConfirmClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_time);

        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

        cancelTextView = findViewById(R.id.dialog_select_time_cancelTextView);
        titleTextView = findViewById(R.id.dialog_select_time_titleTextView);
        confirmTextView = findViewById(R.id.dialog_select_time_confirmTextView);
        tipTextView = findViewById(R.id.dialog_select_time_tipTextView);
        leftRecyclerView = findViewById(R.id.dialog_select_time_leftRecyclerView);
        rightRecyclerView = findViewById(R.id.dialog_select_time_rightRecyclerView);

        LinearLayoutManager leftLayoutManager = new LinearLayoutManager(getContext());
        leftLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        leftRecyclerView.setLayoutManager(leftLayoutManager);
        leftTimeAdapter = new SelectLeftTimeAdapter(getContext());
        leftRecyclerView.setAdapter(leftTimeAdapter);

        LinearLayoutManager rightLayoutManager = new LinearLayoutManager(getContext());
        rightLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rightRecyclerView.setLayoutManager(rightLayoutManager);

        rightTimeAdapter = new SelectRightTimeAdapter(getContext());
        rightRecyclerView.setAdapter(rightTimeAdapter);

        initListener();
        initData();
    }

    private void initData() {
        if (iSelectTimeData != null) {
            titleTextView.setText(iSelectTimeData.getTitle());
            tipTextView.setText(iSelectTimeData.getTip());
            leftTimeAdapter.addAll(iSelectTimeData.getLeftTimeData());
            rightTimeAdapter.addAll(iSelectTimeData.getRightTimeData());
        }
    }

    private void initListener() {
        cancelTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        confirmTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (onSelectTimeConfirmClickListener != null) {
                    onSelectTimeConfirmClickListener.timeConfirm(getLeftSelectTime().getSelectItemTextValue() + " " + getRightSelectTime().getSelectItemTextValue());
                }
            }
        });

        leftTimeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                leftTimeAdapter.setSelectPosition(position);
                clearRightTimeSelect();
            }
        });
        rightTimeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                rightTimeAdapter.setSelectPosition(position);
            }
        });
    }

    private void clearRightTimeSelect() {
        rightTimeAdapter.setSelectPosition(0);
        rightRecyclerView.scrollToPosition(0);
    }

    public ISelectItemValue getLeftSelectTime() {
        return leftTimeAdapter.getSelectTime();
    }

    public ISelectItemValue getRightSelectTime() {
        return rightTimeAdapter.getSelectTime();
    }

    public interface OnSelectTimeConfirmClickListener {
        void timeConfirm(String time);
    }
}
